package web.demos.web.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.endpoint.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import web.demos.web.domain.po.Student;
import web.demos.web.mapper.StudentMapper;
import web.demos.web.service.IStudentService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author k
 * @since 2024-05-11
 */
@Service
@Slf4j
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
    @Autowired
    @Qualifier("JaxWsClient")
    private Client client;

    @Override
    public Student getStudent() {
        Object[] getStudents = new Object[0];
        try {
            getStudents = client.invoke("getStudent", "wudizaba");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (getStudents != null && getStudents.length > 0) {
            Object getStudent = getStudents[0];
            JSON parse = JSONUtil.parse(getStudent);
            Student bean = parse.toBean(Student.class);
            this.save(bean);
            return bean;
        }
        return null;
    }

    @Override
    public List<Student> getAllStudent() {
        Object[] obs = new Object[0];
        ArrayList<Student> list = new ArrayList<>();
        try {
            obs = client.invoke("getAllStudent");
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (obs != null && obs.length > 0) {
            List<Object> ob = (List<Object>) obs[0];
            if (CollectionUtil.isNotEmpty(ob)) {
                for (Object o : ob) {
                    JSON parse = JSONUtil.parse(o);
                    Student bean = parse.toBean(Student.class);
                    list.add(bean);
                }
            }

            if (CollectionUtil.isNotEmpty(list)) {
                return list;
            }
        }

        return Collections.emptyList();
    }
}
